package com.hrb.monkey.frament;


import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;

import com.hrb.commonsdk.okhttp.listener.DisposeDataListener;
import com.hrb.commonsdk.okhttp.request.RequestParams;
import com.hrb.monkey.CustomApplication;
import com.hrb.monkey.R;
import com.hrb.monkey.activity.MainActivity;
import com.hrb.monkey.network.http.RequestCenter;
import com.hrb.monkey.record.UserLoginRecord;
import com.hrb.monkey.utils.ToastUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * A simple {@link Fragment} subclass.
 */
public class LoginFragment extends Fragment {


    @BindView(R.id.loginname_text)
    EditText loginnameText;
    @BindView(R.id.password_text)
    EditText passwordText;
    @BindView(R.id.login_checkbox)
    CheckBox loginCheckbox;
    @BindView(R.id.login_btn)
    Button loginBtn;
    @BindView(R.id.sign_btn)
    Button signBtn;
    Unbinder unbinder;
    FirstFragment firstFragment;
    private  SharedPreferences preferences;
    public LoginFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_login, container, false);
        unbinder = ButterKnife.bind(this, view);

        preferences= getActivity().getSharedPreferences("userInfo",Context.MODE_PRIVATE);
        loginnameText.setText(preferences.getString("username","admin"));
        passwordText.setText(preferences.getString("password",""));
        loginCheckbox.setChecked(preferences.getBoolean("ischecked",false));
        loginCheckbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

            }
        });
        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.login_checkbox, R.id.login_btn, R.id.sign_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.login_checkbox:
                break;
            case R.id.login_btn:
                userlogin();
                //ToastUtil.show( loginnameText.getText().toString()+" : "+passwordText.getText().toString());
                break;
            case R.id.sign_btn:
                break;
        }
    }

    private void userlogin(){
        RequestParams params=new RequestParams();
        params.put("username",loginnameText.getText().toString());
        params.put("password",passwordText.getText().toString());
        RequestCenter.userLogin(params, new DisposeDataListener() {
            @Override
            public void onSuccess(Object responseObj) {
                UserLoginRecord userLoginRecord=(UserLoginRecord) responseObj;
                firstFragment=new FirstFragment();
                //保存登录信息
                saveUserInfo(userLoginRecord);
                startActivity(new Intent(getContext(),MainActivity.class));
                //getFragmentManager().beginTransaction().replace(R.id.content_fragment,firstFragment).show(firstFragment).commit();
                ToastUtil.show(userLoginRecord.getMsg());
            }

            @Override
            public void onFailure(Object reasonObj) {
                UserLoginRecord userLoginRecord= null;
                try {
                    userLoginRecord = (UserLoginRecord) reasonObj;
                    ToastUtil.show(userLoginRecord.getMsg());
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        },UserLoginRecord.class);
    }

    /**
     * 保存登录信息
     * @param userLoginRecord
     */
    private void  saveUserInfo(UserLoginRecord userLoginRecord){
        SharedPreferences.Editor editor=  preferences.edit();
        if(loginCheckbox.isChecked()){
            editor.putString("username",userLoginRecord.getData().getLoginname());
            editor.putString("password",userLoginRecord.getData().getPassword());
            editor.putBoolean("ischecked",true);
            editor.commit();
        }else {
            editor.putString("username","");
            editor.putString("password","");
            editor.putBoolean("ischecked",false);
            editor.commit();
        }

    }


    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if(!hidden){
            loginnameText.setText(preferences.getString("username","admin"));
            if(getArguments()!=null&&getArguments().getString("zcode")!=null)
                loginnameText.setText(getArguments().getString("zcode"));
            getArguments().clear();
        }else{
            loginnameText.setText(preferences.getString("username","admin"));
        }
    }
}
